package com.xique.door.service.impl;


import com.xique.door.bean.domain.PermissionGroupUser;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.service.IPermissionGroupUserDomainService;
import com.xique.door.service.IPermissionGroupUserService;
import com.xique.door.service.IUserDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shd
 * @create 2021/7/7 15:28
 */
@Service
public class PermissionGroupUserDomainServiceImpl implements IPermissionGroupUserDomainService {

    @Autowired
    private IPermissionGroupUserService permissionGroupUserService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean deleteUserInfos(List<Long> permissionGroupUserIds, List<Long> userDeviceIds) {

        int count = permissionGroupUserService.batchDeletePermissionGroupUser(permissionGroupUserIds);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchDeleteUserDevice(userDeviceIds);
        if (count <= 0) {
            return false;
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean deleteUserInfo(Long permissionGroupUserIds, List<Long> userDeviceIds) {

        int count = permissionGroupUserService.deletePermissionGroupUser(permissionGroupUserIds);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchDeleteUserDevice(userDeviceIds);
        if (count <= 0) {
            return false;
        }

        return true;
    }

    @Override
    public boolean addUserInfos(List<PermissionGroupUser> permissionGroupUsers, List<UserDevice> userDevices) {

        int count = permissionGroupUserService.batchInsertPermissionGroupUser(permissionGroupUsers);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchInsertUserDevice(userDevices);
        if (count <= 0) {
            return false;
        }

        return true;
    }
}
